import { Component, Injector } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { FileObjectConfigDto, FileUploadServiceProxy, UpFileInfoInput } from '@shared/service-proxies/service-proxies';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { merge as _merge } from 'lodash-es';
import { finalize } from 'rxjs/operators';
import { FileUploadHelper } from '@shared/helpers/FileUploadHelper';

export interface IFileUpdateModalOptions {
    title?: string;
    multiply: boolean;
    customKey: string;
    config: FileObjectConfigDto;
    id: string;
    autoSyncBySelf: boolean;
}

//For more modal options http://valor-software.com/ngx-bootstrap/#/modals#modal-directive

@Component({
    templateUrl: './file-update-modal.component.html'
})
export class FileUpdateModalComponent extends AppComponentBase {

    reloadList: {():void};

    options: IFileUpdateModalOptions ;

    uploadType: number = 0;

    isShown = false;
    isInitialized = false;
    isMultiply = false;

    loading = false;

    saving: boolean = false;

    config: FileObjectConfigDto;

    data: UpFileInfoInput;

    dataSynchronous: boolean = false;

    constructor(
        injector: Injector,
        public bsModalRef: BsModalRef,
        private _fileUploadServiceProxy: FileUploadServiceProxy
    ) {
        super(injector);
        this.dataSynchronous = this.setting.getBoolean("App.Synchronous.DataSynchronous");

        this.data = new UpFileInfoInput();
    }

    configure(options: IFileUpdateModalOptions): void {
        this.options = options;
        this.isMultiply = this.options.multiply;
        this.uploadType = this.isMultiply ? 1 : 0;
        this.config = this.options.config;

    }

    close(): void {
        this.bsModalRef.hide();
    }

    shown(): void {
        this.isShown = true;
        this.init();
    }

    init(): void {
        if (this.options.id) {
            this.loading = true;
            this._fileUploadServiceProxy.getFileInfoById(this.options.id)
                .pipe(finalize(() => { this.loading = false; }))
                .subscribe(result => {
                    if (result) {
                        this.data = new UpFileInfoInput({
                            id: result.id,
                            title: result.title,
                            description: result.description,
                            linkUrl: result.linkUrl,
                            order: result.order,
                            isActive: result.isActive,
                            autoSynchronous: result.autoSynchronous,
                            alt: result.alt,
                            source: result.scoure,
                            alias: this.config.alias,
                            customKey: this.options.customKey,
                            //defaultKey: this.options.defaultKey,
                            //uploadType: this.uploadType,
                            fileUrl: result.fileUrl,
                            preViewImgUrl: result.preViewImage
                        });
                    }
                });
        }
        this.isInitialized = true;
    }

    save() {
        if (this.saving) {
            return;
        }
        this.saving = true;

        this._fileUploadServiceProxy.updateFileInfo(this.data)
            .pipe(finalize(() => {
                this.saving = false;
            }))
            .subscribe(result => {
                this.notify.success(this.l('SavedSuccessfully'));
                this.reloadList && this.reloadList();
                this.close();
            });
    }

    getExName(fileName) {
        if (fileName)
            return FileUploadHelper.getExName(fileName);
        return '';
    };
}
